'use strict'; const { Document } = require('/document'); const { ShapeNodeDefinition } = require('/nodes'); const { ShapeStar } = require('/shapes'); const { Colour } = require('/colours'); const { FillDescriptor } = require('/fills'); const { AddChildNodesCommandBuilder } = require('/commands'); const { Dialog, DialogResult, UnitType } = require('/dialog.js'); const doc = Document.current; function isOk(result) {return (result?.value ?? result) === DialogResult.Ok.value;} function randFloat(min, max) {return Math.random() * (max - min) + min;} function randInt(min, max) {return Math.floor(Math.random() * (max - min + 1)) + min;} if (!doc) { console.log('ERROR: No document open'); } else { const COUNT = 400; const CANVAS_W = 4000; const CANVAS_H = 4000; const MAX_W = 400; const MAX_H = 600; const MIN_SIZE = 60; // minimum size so stars are visible const dlg = Dialog.create('Create stars'); const col = dlg.addColumn(); const g1 = col.addGroup(''); const shapeCtrl = g1.addUnitValueEditor('Stars', UnitType.Number, UnitType.Number, COUNT, 1, 2000).setPrecision(0); const rangexCtrl = g1.addUnitValueEditor('Horizontal range', UnitType.Number, UnitType.Number, CANVAS_W, 0, 10000).setPrecision(2); const rangeyCtrl = g1.addUnitValueEditor('Vertical range', UnitType.Number, UnitType.Number, CANVAS_H, 0, 10000).setPrecision(2); const widthCtrl = g1.addUnitValueEditor('Width (x)', UnitType.Number, UnitType.Number, MAX_W, 1, 5000).setPrecision(2); const heightCtrl = g1.addUnitValueEditor('Height (y)', UnitType.Number, UnitType.Number, MAX_H, 1, 5000).setPrecision(2); const minsizeCtrl = g1.addUnitValueEditor('Min Size', UnitType.Number, UnitType.Number, MIN_SIZE, 1, 5000).setPrecision(2); const pointsCtrl = g1.addUnitValueEditor('Points', UnitType.Number, UnitType.Number, 10, 3, 100).setPrecision(0); const innercircleCtrl = g1.addUnitValueEditor('Inner Circle', UnitType.Number, UnitType.Number, 0, 0, 100).setPrecision(2); const outercircleCtrl = g1.addUnitValueEditor('Outer Circle', UnitType.Number, UnitType.Number, 0, 0, 100).setPrecision(2); const rCtrl = g1.addUnitValueEditor('Red factor', UnitType.Number, UnitType.Number, 1, 0.1, 3).setPrecision(2); const gCtrl = g1.addUnitValueEditor('Green factor', UnitType.Number, UnitType.Number, 1, 0.1, 3).setPrecision(2); const bCtrl = g1.addUnitValueEditor('Blue factor', UnitType.Number, UnitType.Number, 1, 0.1, 3).setPrecision(2); const linkCtrl = g1.addSwitch("Link shape xy", false) const randomCtrl = g1.addSwitch("Random shape size", true) const randompointsCtrl = g1.addSwitch("Random points", true) const randominnerCtrl = g1.addSwitch("Random inner radius", false) const randomouterCtrl = g1.addSwitch("Random outer radius", false) dlg.initialWidth = 380; const result = dlg.runModal(); if (isOk(result)) { const builder = AddChildNodesCommandBuilder.create(); for (let i = 0; i < shapeCtrl.value; i++) { // Random dimensions within max bounds let w = widthCtrl.value; let h = heightCtrl.value; // randomize the size of the stars if (randomCtrl.value) { w = randFloat(minsizeCtrl.value, widthCtrl.value); h = randFloat(minsizeCtrl.value, heightCtrl.value); } // if link star size is set then set them both to the height (or whatever) if (linkCtrl.value) {h=w;} // Random position const x = randFloat(0, rangexCtrl.value); const y = randFloat(0, rangeyCtrl.value); // Random points - use the value given or randomize between 3 and points (no point going lower though) let points = pointsCtrl.value; if (randompointsCtrl.value) { points = randInt(3, pointsCtrl.value); } // Random inner radius in px, converted to proportion of half the smaller dimension const innerRadiusPx = randFloat(10, 50); const halfMinDim = Math.min(w, h) / 2; // innerRadius is a proportion 0–1 of half the bounding box const innerRadiusProportion = Math.min(innerRadiusPx / halfMinDim, 0.85); // Random colour const r = Math.floor(rCtrl.value * Math.random() * 256); const g = Math.floor(gCtrl.value * Math.random() * 256); const b = Math.floor(bCtrl.value * Math.random() * 256); const colour = Colour.createRGBA8({ r, g, b, alpha: 255 }); const fill = FillDescriptor.createSolid(colour); // Build the star shape const star = ShapeStar.create(); star.points = points; star.innerRadius = innerRadiusProportion; star.circleInnerRadius = innercircleCtrl.value; star.circleOuterRadius = outercircleCtrl.value; const rect = { x, y, width: w, height: h }; const def = ShapeNodeDefinition.create(star, rect, fill, null, null, null); builder.addShapeNode(def); } const cmd = builder.createCommand(true); doc.executeCommand(cmd); console.log('Random stars created!'); } }